<#include "/includes/vars.inc"> 
<#assign packageDir = project.@rootPackage?replace(".","/")>
<@pp.dropOutputFile />
<#assign fileName = "${javaDir}${packageDir}/web/support/LoginController.java">
<#if includeLoginJsf && !pp.outputFileExists(fileName)>
	<@pp.changeOutputFile name="${fileName}" />

<#list entities.entity as entity>
    <#if entity.@name = config.login.@entity>
	<#assign entityName = entity.@name>
		
package ${project.@rootPackage}.web.support;

import ${project.@rootPackage}.web.util.JsfUtil;
import ${project.@rootPackage}.domain.${entityName};
<#list entity.column.@type as enum>
    <#if enums.enum.@name?seq_contains(enum)>
import ${project.@rootPackage}.domain.enums.${enum};
    </#if>
</#list>
import ${project.@rootPackage}.service.${entityName}Service;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * LoginController
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
@Controller(value="loginController") 
@Scope("session")
public class LoginController implements Serializable {

	protected static final Logger logger = Logger.getLogger("LoginController");
    private static final long serialVersionUID = 1L;
    
    private ${entityName} current;
    
    @Autowired
    private ${entityName}Service service;
    
    @PostConstruct
    public void init(){
        //Create admin current if table currents is empty
        if (service.findAll().isEmpty()){
            ${entityName} current = new ${entityName}();
            current.setLogin("admin");
            current.setPassword("admin");
            current.setRol(Rol.ROL_ADMIN);
            if (service.create(current)){
                 logger.log(Level.INFO, "${entityName} table is empty,admin user created");
            }            
        }
    }
 
    public void login() {
        try {
            CredentialsController credentials = (CredentialsController) JsfUtil.getBean("credentialsController");
            String login = credentials.getLogin();
            String password = credentials.getPassword();
            //System.out.println("login: "+ login + " " + password);

            if (login != null && password != null) {
                ${entityName} res = service.findByLoginAndPassword(login, password);
                if (res != null) {
                    current = res;
                    logger.log(Level.INFO, "Login: {0} {1} ", new Object[]{login,new Date()});
                    JsfUtil.addSuccessMessage(JsfUtil.getMessage("loginInfo") + " " + current.getLogin());
                } else {
                	logger.log(Level.SEVERE, "Login: {0} {1} ", new Object[]{login,password});
                    JsfUtil.addErrorMessage("formLogin:login", JsfUtil.getMessage("loginError"));
                    current = null;
                }
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, e.getMessage());
        }
    }

    public void logout() {
        logger.log(Level.INFO, "Logout: {0} {1}", new Object[]{current.getLogin(),new Date()});
        current = null;
        JsfUtil.closeSession();
        JsfUtil.redirect("/login.xhtml");
    }

    public boolean isLoggedIn() {
        return current != null;
    }
    
    public String get${entityName}Name() {
        return current.getLogin();
    }
    
    public ${entityName} get${entityName}() {
        return current;
    }


    public boolean isRoleAdmin() {
        return isLoggedIn() && current.getRol().equals(Rol.ROL_ADMIN);
    }
  
}

	</#if>
</#list>

</#if>
